package com.ctosb.classviewer.clazz.attr;

import com.ctosb.classviewer.ClassReader;
import com.ctosb.classviewer.clazz.AbstractParse;
import com.ctosb.classviewer.clazz.Parseable;
import com.ctosb.classviewer.clazz.cp.CONSTANT_Utf8_info;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class Annotation extends AbstractParse {

    private short type_index;
    private short num_element_value_pairs;
    private Element_value_pair element_value_pairs[];

    @Override
    public Annotation parse(ClassReader classReader) {
        this.type_index = classReader.readShort();
        this.num_element_value_pairs = classReader.readShort();
        element_value_pairs = new Element_value_pair[num_element_value_pairs & 0xffff];
        for (int i = 0; i < element_value_pairs.length; i++) {
            element_value_pairs[i] = new Element_value_pair().parse(classReader, getClassFile());
        }
        return this;
    }

    /**
     * 字段描述符，表示annotation描述
     * @Author alan
     * @Date 2020/12/20 11:18 下午
     * @return
     */
    public String getType() {
        return ((CONSTANT_Utf8_info)getClassFile().getConstant_pool()[type_index]).getValue();
    }
}